package com.widget.wallpaper;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class inputActivity extends Activity{
	private static final String PREFS_NAME = "com.widget.wallpaper.WNWProvider";
	private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
	private String mBackground = "";
	private static final String PREF_CHAR_KEY = "wallpaper";
	public static final String PREF_CHARACTER = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input);

		
		
		// Find the widget id from the intent.
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			
		}
		
		
		// Bind the action for the save button.
		findViewById(R.id.button1).setOnClickListener(mOnClickListener);
		
		CharSequence backgroundCode = inputActivity.getBackgroundPref(inputActivity.this);
		EditText textBox = (EditText)findViewById(R.id.editText1);
		textBox.setText(backgroundCode);

		final Context context = inputActivity.this;


	}
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			final Context context = inputActivity.this;
			EditText edit_text = (EditText)findViewById(R.id.editText1);
	           CharSequence edit_text_value =edit_text.getText();
	         //  Log.d("charactor",edit_text_value+"edit_text_value");
	           mBackground = edit_text_value+"";
			// When the button is clicked, save our prefs and return that they
			// clicked OK.

			saveBackgroundPref(context, mAppWidgetId, mBackground);

			// Push widget update to surface with newly set prefs
			//context.startService(new Intent(context, WNWService.class));
			Intent iStartService =new Intent(context, WNWService.class);
			iStartService.putExtra("INTENT_SHOW_INPUT", "TRUE");
			context.startService(iStartService);

			// Make sure we pass back the original appWidgetId
			Intent resultValue = new Intent();
			resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					mAppWidgetId);
			

			setResult(RESULT_OK, resultValue);
			finish();
			
		}


	};
	private void saveBackgroundPref(Context context, int mAppWidgetId,
			String mBackground) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, 0).edit();
		prefs.putString(PREF_CHAR_KEY, mBackground);
		prefs.commit();
		
		
	}
	public static String getBackgroundPref(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
		//Log.d("charater","getBackgroud");
		return prefs.getString(PREF_CHAR_KEY, PREF_CHARACTER);
		
	}
	
}


